package com.sqfw.project.wpReport.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableSupport;
import com.sqfw.project.wpReport.domain.SeatServiceReport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wpReport.domain.SeatEvaluateReport;
import com.sqfw.project.wpReport.service.ISeatEvaluateReportService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 后台坐席评价汇总表Controller
 * 
 * @author lx
 * @date 2023-07-26
 */
@Slf4j
@RestController
@RequestMapping("/wpReport/seatEvaluateReport")
public class SeatEvaluateReportController extends BaseController
{
    @Autowired
    private ISeatEvaluateReportService seatEvaluateReportService;

    /**
     * 查询后台坐席评价汇总表列表
     */
    @PreAuthorize("@ss.hasPermi('getSeatEvaluateReportList')")
    @GetMapping("/getSeatEvaluateReportList")
    public TableDataInfo getSeatEvaluateReportList(SeatEvaluateReport seatEvaluateReport)
    {
        startPage();
        long l = System.currentTimeMillis();
        TableDataInfo tableDataInfo = seatEvaluateReportService.getSeatEvaluateReportList(seatEvaluateReport);
        log.info("坐席-流转工单得分表运行时间：{}",System.currentTimeMillis()-l);
        long total = tableDataInfo.getTotal();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        if ((long) pageNum * pageSize >= total && total > 0) {
            List<SeatEvaluateReport> list = (List<SeatEvaluateReport>) tableDataInfo.getRows();
            if (list.size()>0){
                TableDataInfo tableDataInfoCon = seatEvaluateReportService.getSeatEvaluateReportList(seatEvaluateReport);
                List<SeatEvaluateReport> reportList = (List<SeatEvaluateReport>) tableDataInfoCon.getRows();
                SeatEvaluateReport report = new SeatEvaluateReport();

                report.setCode("总计");
                //12345工单数量
                int wpCount = reportList.stream()
                        .map(SeatEvaluateReport::getWpCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setWpCount(wpCount);
                //总分
                int countPoint = reportList.stream()
                        .map(SeatEvaluateReport::getCountPoint)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setCountPoint(countPoint);
                //总扣分
                int countDeductPoint = reportList.stream()
                        .map(SeatEvaluateReport::getCountDeductPoint)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setCountDeductPoint(countDeductPoint);
                //勾选错误
                int checkPoint = reportList.stream()
                        .map(SeatEvaluateReport::getCheckPoint)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setCheckPoint(checkPoint);

                //格式错误
                int formatPoint = reportList.stream()
                        .map(SeatEvaluateReport::getFormatPoint)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setFormatPoint(formatPoint);

                //过程不明晰
                int processPoint = reportList.stream()
                        .map(SeatEvaluateReport::getProcessPoint)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setProcessPoint(processPoint);

                //其他
                int otherPoint = reportList.stream()
                        .map(SeatEvaluateReport::getOtherPoint)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setOtherPoint(otherPoint);

                //总得分
                int totalPoint = reportList.stream()
                        .map(SeatEvaluateReport::getTotalPoint)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setTotalPoint(totalPoint);

                list.add(report);
            }


        }
        return tableDataInfo;
    }

    /**
     * 导出后台坐席评价汇总表列表
     */
    @Log(title = "后台坐席评价汇总表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('getSeatEvaluateReportList:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SeatEvaluateReport seatEvaluateReport)
    {
        TableDataInfo tableDataInfo = seatEvaluateReportService.getSeatEvaluateReportList(seatEvaluateReport);
        List<SeatEvaluateReport> list = (List<SeatEvaluateReport>) tableDataInfo.getRows();
        if (list.size()>0){
            SeatEvaluateReport report = new SeatEvaluateReport();

            report.setCode("总计");
            //12345工单数量
            int wpCount = list.stream()
                    .map(SeatEvaluateReport::getWpCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setWpCount(wpCount);
            //总分
            int countPoint = list.stream()
                    .map(SeatEvaluateReport::getCountPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setCountPoint(countPoint);
            //总扣分
            int countDeductPoint = list.stream()
                    .map(SeatEvaluateReport::getCountDeductPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setCountDeductPoint(countDeductPoint);
            //勾选错误
            int checkPoint = list.stream()
                    .map(SeatEvaluateReport::getCheckPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setCheckPoint(checkPoint);

            //格式错误
            int formatPoint = list.stream()
                    .map(SeatEvaluateReport::getFormatPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setFormatPoint(formatPoint);

            //过程不明晰
            int processPoint = list.stream()
                    .map(SeatEvaluateReport::getProcessPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setProcessPoint(processPoint);

            //总得分
            int totalPoint = list.stream()
                    .map(SeatEvaluateReport::getTotalPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setTotalPoint(totalPoint);

            //平均得分
            int averagePoint = list.stream()
                    .map(SeatEvaluateReport::getAveragePoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setAveragePoint(averagePoint);

            list.add(report);
        }
        ExcelUtil<SeatEvaluateReport> util = new ExcelUtil<SeatEvaluateReport>(SeatEvaluateReport.class);
        util.exportExcel(response, list, "后台坐席评价汇总表数据");
    }


}
